<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 一、随机验证码（必须包含小写，大写，数字）

        function randomNum(min, max) {
            return parseInt(Math.random() * (max - min) + min);
        }

        function randCode(n) {
            var arrNum = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
            var arrUpper = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
            var arrLower = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
            var arr = arrNum.concat(arrUpper, arrLower);

            var str = "";

            str += arrNum[randomNum(0, arrNum.length)];
            str += arrUpper[randomNum(0, arrUpper.length)];
            str += arrLower[randomNum(0, arrLower.length)];
            // str += arr[randomNum(0, arr.length)];
            for (var m = 0; m < n - 3; m++) {
                // var num = arr[randomNum(0, arr.length)]
                // console.log(num);
                str += arr[randomNum(0, arr.length)];
            }
            // console.log(str);
            return str;
        }

        console.log(randCode(4));

        // concat封装

        // 三、去重

        var arR = [8439, 657, 12, 77, 3421, 2, 3, 4, 1, 2, 3, 8439, 657, 12, 77, 3424, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 8439, 657, 12, 77, 3472, 88];

        // filter封装去重

        function fil(arr) {
            var arr2 = arr.sort(function (a, b) {
                return a - b;
            });
            // console.log(arr2);
            var newarr = arr2.filter(function (item, m, newarr) {
                return newarr.indexOf(item) === m;
            })

            // console.log(newarr);
            return newarr;
        }
        console.log(fil(arR));

        // 四、统计
        function counNum() {
            var arr2 = [8439, 657, 12, 77, 3421, 2, 3, 4, 1, 2, 3, 8439, 657, 12, 77, 3424, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 8439, 657, 12, 77, 3472, 88];

            var arr = arr2.sort(function (a, b) {
                return a - b;
            })

            for (var m = 0; m < arr.length; m++) {
                var count = 1;
                for (var n = m + 1; n < arr.length; n++) {
                    if (arr[m] === arr[n]) {
                        count++;
                        arr.splice(n, 1);
                        n--;
                    }
                }
                console.log(arr[m] + "出现了" + count + "次");
            }
        }
        counNum();

        // 五、在一个已经排序好的数组中插入一个值,   [1,4,6,9,12]  10
        // 法一：
        var arry = [1, 4, 6, 9, 12];
        function insert(num) {
            // var num = 7;

            if (num < arry[0]) {
                arry.unshift(num)
            } else if (num > arry[arry.length - 1]) {
                arry.push(num);
            } else {
                for (var m = 0; m < arry.length; m++) {
                    if (num >= arry[m] && num <= arry[m + 1]) {
                        arry.splice(m + 1, 0, num);
                        break;
                    }
                }
            }

            // console.log(arry);
            return arry;
        }
        console.log(insert(10));

        // 法二：push+sort
        var arry1 = [1, 4, 6, 9, 12, 21, 44, 3, 45, 4545, 1, 7];
        var arry2 = [12, 43, 5656, 78]

        function insert2(arry, arry2) {
            arry2.forEach(function (item, m) {
                arry1.push(arry2[m]);
            })

            var arr = arry1.sort(function (a, b) {
                return a - b;
            })

            // console.log(arr);
            return arr;
        }
        console.log(insert2(arry1, arry2));


    </script>
</body>

</html>